#define _CRT_SECURE_NO_WARNINGS #include #include #include typedef struct { char name[100]; } element; typedef struct ListNode { element data; struct ListNode* link; } ListNode; element delete_first(ListNode* head) { ListNode* removed; element data; if (head == (ListNode*)NULL) return; removed = head->link; data = removed->data; if (head == removed) head = NULL; else head->link = removed->link; free(removed); return data; } ListNode* insert_last(ListNode* head, element data) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->data = data; if (head == NULL) { head = node; node->link = head; } else { node->link = head->link; head->link = node; head = node; } return head; } // ¸®½ºÆ®ÀÇ Ç׸ñ Ãâ·Â void print_list(ListNode* head) { ListNode* p; if (head == NULL) return; // µ¥ÀÌÅÍ°¡ ¾øÀ» ¶§ if (head == head->link) { // µ¥ÀÌÅÍ°¡ 1°³ ÀÏ ¶§ printf("%s->\n", head->data); } else { // µ¥ÀÌÅÍ°¡ 2°³ ÀÌ»óÀÏ ¶§ p = head->link; do { printf("%s->", p->data); p = p->link; } while (p != head); printf("%s->\n", p->data); // ¸¶Áö¸· ³ëµå Ãâ·Â } } int main(void) { ListNode* head = NULL; element data; strcpy(data.name, "KIM"); head = insert_last(head, data); print_list(head); strcpy(data.name, "PARK"); head = insert_last(head, data); print_list(head); strcpy(data.name, "CHOI"); head = insert_last(head, data); print_list(head); data = delete_first(head); printf("%s\n", data.name); data = delete_first(head); printf("%s\n", data.name); data = delete_first(head); printf("%s\n", data.name); return 0; }